//
//  JSQMessageFunctionView.m
//  Conversation
//
//  Created by C_HAO on 15/9/23.
//  Copyright © 2015年 CHAOO. All rights reserved.
//

#import "JSQMessageFunctionView.h"



@interface JSQMessageFunctionView ()

@property(nonatomic, copy) NSArray *views;

@property(nonatomic, strong) NSLayoutConstraint *voiceStartLayoutAttributeHeight;
@property(nonatomic, strong) NSLayoutConstraint *voiceEndLayoutAttributeHeight;

@property(nonatomic, strong) NSLayoutConstraint *categoryStartLayoutAttributeHeight;
@property(nonatomic, strong) NSLayoutConstraint *categoryEndLayoutAttributeHeight;

@property(nonatomic, strong) NSLayoutConstraint *emoticonStartLayoutAttributeHeight;
@property(nonatomic, strong) NSLayoutConstraint *emoticonEndLayoutAttributeHeight;

@end

@implementation JSQMessageFunctionView

- (void)awakeFromNib {
    [super awakeFromNib];

    NSArray *nibViews = [[NSBundle bundleForClass:[JSQVoiceView class]] loadNibNamed:NSStringFromClass([JSQVoiceView class]) owner:nil options:nil];
    _voiceView = (JSQVoiceView *)nibViews.firstObject;
    [self addSubview:_voiceView];
    _voiceView.translatesAutoresizingMaskIntoConstraints = NO;

    _voiceStartLayoutAttributeHeight = [NSLayoutConstraint constraintWithItem:_voiceView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
    _voiceEndLayoutAttributeHeight = [NSLayoutConstraint constraintWithItem:_voiceView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1 constant:0];

    [self addConstraint:_voiceEndLayoutAttributeHeight];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_voiceView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_voiceView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_voiceView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];

    _emoticonView = [[JSQEmoticonView alloc] init];
    [self addSubview:_emoticonView];
    _emoticonView.translatesAutoresizingMaskIntoConstraints = NO;

    _emoticonStartLayoutAttributeHeight = [NSLayoutConstraint constraintWithItem:_emoticonView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
    _emoticonEndLayoutAttributeHeight = [NSLayoutConstraint constraintWithItem:_emoticonView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1 constant:0];

    [self addConstraint:_emoticonEndLayoutAttributeHeight];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_emoticonView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];

    nibViews = [[NSBundle bundleForClass:[JSQCategoryView class]] loadNibNamed:NSStringFromClass([JSQCategoryView class]) owner:nil options:nil];
    _categoryView = (JSQCategoryView *)nibViews.firstObject;
    [self addSubview:_categoryView];
    _categoryView.translatesAutoresizingMaskIntoConstraints = NO;

    _categoryStartLayoutAttributeHeight = [NSLayoutConstraint constraintWithItem:_categoryView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
    _categoryEndLayoutAttributeHeight = [NSLayoutConstraint constraintWithItem:_categoryView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1 constant:0];

    [self addConstraint:_categoryEndLayoutAttributeHeight];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_categoryView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_categoryView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_categoryView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];

    _views = @[_voiceView, _emoticonView, _categoryView];
    _messageFunctionType = JSQMessageFunctionNone;
}

- (void)setMessageFunctionType:(JSQMessageFunctionType)messageFunctionType {
    _messageFunctionType = messageFunctionType;

    if (messageFunctionType == JSQMessageFunctionNone) {
        return;
    }
    UIView *exampleViewToShow = _views[messageFunctionType];

    [self bringSubviewToFront:exampleViewToShow];

    [UIView animateWithDuration:0.2 animations:^{
        if (exampleViewToShow == _emoticonView) {
            [self removeConstraint:_emoticonStartLayoutAttributeHeight];
            [self addConstraint:_emoticonEndLayoutAttributeHeight];
        } else if (exampleViewToShow == _categoryView) {
            [self removeConstraint:_categoryStartLayoutAttributeHeight];
            [self addConstraint:_categoryEndLayoutAttributeHeight];
        } else if (exampleViewToShow == _voiceView) {
            [self removeConstraint:_voiceStartLayoutAttributeHeight];
            [self addConstraint:_voiceEndLayoutAttributeHeight];
        }
        [self layoutIfNeeded];
    }];
}

- (void)showFunctionType:(JSQMessageFunctionType)functionType {
    if (self.messageFunctionType != functionType) {

        if (functionType == JSQMessageFunctionVoice) {
            [self removeConstraint:_voiceEndLayoutAttributeHeight];
            [self addConstraint:_voiceStartLayoutAttributeHeight];
        } else if (functionType == JSQMessageFunctionEmotion) {
            [self removeConstraint:_emoticonEndLayoutAttributeHeight];
            [self addConstraint:_emoticonStartLayoutAttributeHeight];
        } else if (functionType == JSQMessageFunctionOther) {
            [self removeConstraint:_categoryEndLayoutAttributeHeight];
            [self addConstraint:_categoryStartLayoutAttributeHeight];
        }
        [self layoutIfNeeded];

        self.messageFunctionType = functionType;
    }
}

@end
